/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.orhanobut.demo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer.LayoutConfig;
import ohos.agp.components.PositionLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.render3d.BuildConfig;
import ohos.agp.utils.Color;
import ohos.hiviewdfx.HiLogLabel;

import java.util.*;

import com.orhanobut.logger.HarmonyLogAdapter;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

/**
 * MainAbilitySlice class which do all logger sample app logic
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG_LOG = "[Logger Sample] ";
    private static final int DOMAIN_ID = 0xD000F00;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);
    private static final String LOG_FORMAT = "%s: %s";
    private PositionLayout myLayout = new PositionLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setShape(ShapeElement.RECTANGLE);
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        Text text = new Text(this);
        text.setText("Hello World");
        text.setTextColor(Color.BLACK);
        myLayout.addComponent(text);

        super.setUIContent(myLayout);

        Logger.addLogAdapter(new HarmonyLogAdapter());
        Logger.d("message");

        Logger.clearLogAdapters();

        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0) // (Optional) How many method line to show. Default 2
                .methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
                .tag("Custom tag")   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();

        Logger.addLogAdapter(new HarmonyLogAdapter(formatStrategy));

        Logger.addLogAdapter(new HarmonyLogAdapter() {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });

        Logger.addLogAdapter(new DiskLogAdapter());

        Logger.w("no thread info and","only 1 method");

        Logger.clearLogAdapters();
        formatStrategy = PrettyFormatStrategy.newBuilder().showThreadInfo(false).methodCount(0).build();

        Logger.addLogAdapter(new HarmonyLogAdapter(formatStrategy));
        Logger.i("no thread info and","method info");

        Logger.t("tag").e("Custom tag for only one use","2nd argument");

        Logger.json("{ \"key\": 3, \"value\": something}");

        Logger.f(Arrays.asList("foo1", "bar1"),Arrays.asList("foo2", "bar2"));

        Map<String, String> map = new HashMap<>();
        map.put("key", "value");
        map.put("key1", "value2");

        Logger.f(map,map);

        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
                System.lineSeparator()+"<name id=\"1\"><person>name</person>"+
                System.lineSeparator()+"<city>city</city></name>";

        Logger.xml(xmlStr);

        double[] myList = {1.9, 2.9, 3.4, 3.5};
        Logger.wtf(myList,myList);

        Set<String> set = new HashSet<>();
        set.add("key1");
        set.add("value1");

        Logger.e(set,"hi");

        Logger.clearLogAdapters();
        formatStrategy = PrettyFormatStrategy.newBuilder().showThreadInfo(false).methodCount(0).tag("MyTag").build();
        Logger.addLogAdapter(new HarmonyLogAdapter(formatStrategy));

        Logger.w("my log message with my tag","tag2");

        Multi t1=new Multi();
        t1.start();

    }

    private class Multi extends Thread{
        public void run(){
            Logger.d("thread is running...");
            Logger.addLogAdapter(new HarmonyLogAdapter());
            Logger.d("message thread log");

            Logger.clearLogAdapters();

            FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                    .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                    .methodCount(0) // (Optional) How many method line to show. Default 2
                    .methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
                    .tag("Custom tag thread tag")   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                    .build();

            Logger.addLogAdapter(new HarmonyLogAdapter(formatStrategy));

            Logger.addLogAdapter(new HarmonyLogAdapter() {
                @Override
                public boolean isLoggable(int priority, String tag) {
                    return BuildConfig.DEBUG;
                }
            });

            Logger.addLogAdapter(new DiskLogAdapter());

            Logger.w("no thread info and only 1 method thread log");

            Logger.clearLogAdapters();
            formatStrategy = PrettyFormatStrategy.newBuilder().showThreadInfo(false).methodCount(0).build();

            Logger.addLogAdapter(new HarmonyLogAdapter(formatStrategy));
            Logger.i("no thread info and method info thread log");

            Logger.t("thread tag").e("Custom tag for only one use thread log");

            Logger.json("{ \"key\": 3, \"value\": threadlog}");


            Logger.f(Arrays.asList("foo", "thread log"));

            Map<String, String> map = new HashMap<>();
            map.put("key", "thread log");
            map.put("key1", "thread log2");

            Logger.f(map);

            String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
                    System.lineSeparator()+"<name id=\"1\"><person>thread log</person>"+
                    System.lineSeparator()+"<city>city</city></name>";

            Logger.xml(xmlStr);

            double[] myList = {1.9, 2.9, 3.4, 3.5};
            Logger.w(myList);

            Set<String> set = new HashSet<>();
            set.add("key1");
            set.add("threadlog");

            Logger.e(set);

            Logger.clearLogAdapters();
            formatStrategy = PrettyFormatStrategy.newBuilder().showThreadInfo(false).methodCount(0).tag("MyTag thread Tag").build();
            Logger.addLogAdapter(new HarmonyLogAdapter(formatStrategy));

            Logger.w("my log message with my tag thread log");
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}


